﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;

public partial class sysadmin_DL_Project_AddCourseType : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOCourseType objCourseType = new TOCourseType();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();
            BinderSchoolStage();
            BinderCatalog();
            BinderTrainType();
            BinderCheckBoxList(int.Parse(ddl_XueDuan.SelectedValue), int.Parse(ddl_XueKe.SelectedValue), int.Parse(ddl_TrainType.SelectedValue));
    
            int CTID = 0; 
            if (Request.QueryString["CTID"] != null)
            {
                CTID = int.Parse(Request.QueryString["CTID"].ToString());
                ViewState["CTID"] = CTID.ToString();
                BinderForm(CTID);
            }

        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }


    }
    protected override void InitPage()
    {
        //lbl_InfoTitle.Text = GetLanageValueByKey("lbl_AddJob_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobName.Text = GetLanageValueByKey("lbl_AddJob_JobName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_JobDescription.Text = GetLanageValueByKey("lbl_AddJob_JobDescription", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        //base.InitPage();

        //lbl_InfoTitle_AddNewType.Text = GetLanageValueByKey("lab_AddNewType_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_NewTypeName.Text = GetLanageValueByKey("lab_AddNewType_TypeName", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    //绑定学段
    private void BinderSchoolStage()
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_XueDuan.DataSource = dt;
        ddl_XueDuan.DataTextField = "SchoolStage";
        ddl_XueDuan.DataValueField = "ID";
        ddl_XueDuan.DataBind();

    }

    //绑定学科
    private void BinderCatalog()
    {
        Catalog obj = new Catalog();
        string search = "";
        DataTable dt = obj.QueryCatalog(search);

        ddl_XueKe.DataSource = dt;
        ddl_XueKe.DataTextField = "CatalogName";
        ddl_XueKe.DataValueField = "CatalogId";
        ddl_XueKe.DataBind();

    }

    //绑定培训类型
    private void BinderTrainType()
    {
        TOTrainType obj = new TOTrainType();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_TrainType.DataSource = dt;
        ddl_TrainType.DataTextField = "TrainTypeName";
        ddl_TrainType.DataValueField = "ID";
        ddl_TrainType.DataBind();

    }

    private void BinderCheckBoxList(int xueduan, int xueke,int TrainType)
    {
        Course obj = new Course();
        VOTOCourseType vo = new VOTOCourseType();
        string search = " 1=1";
        if (xueduan != 0)
            search += " and Xueduan="+xueduan.ToString();
        if (xueke != 0)
            search += " and Xueke="+xueke.ToString();
        if (TrainType != 0)
            search += string.Format(" and TrainType like '%,{0},%'" , TrainType.ToString().Trim());

        //CheckBoxList chkList = (CheckBoxList)rptTrainClassIDlist.Items[i].FindControl("chk_FKNewTrainClaIDlist");
        //Label lbl_ProID = (Label)rptTrainClassIDlist.Items[i].FindControl("lbl_ProID");
        DataTable dt1 = obj.QueryCourse(search);

        chk_FKCourseIDlist.DataSource = dt1;
        chk_FKCourseIDlist.DataTextField = "CourseName";
        chk_FKCourseIDlist.DataValueField = "Course_Id";
        chk_FKCourseIDlist.DataBind();

    }

    private string getCourseIDlist()
    {
        string CourseIDlist = "-1,";
       // CheckBoxList chkList = (CheckBoxList)rptTrainClassIDlist.Items[i].FindControl("chk_FKNewTrainClaIDlist");
        for (int j = 0; j < chk_FKCourseIDlist.Items.Count; j++)
        {
            if (chk_FKCourseIDlist.Items[j].Selected)
            {
                CourseIDlist += chk_FKCourseIDlist.Items[j].Value + ",";
            }
        }

        CourseIDlist += "0";
        if (CourseIDlist == "-1,0")
            CourseIDlist = "";
        return CourseIDlist;
    }


    private void BinderForm(int id)
    {
        VOTOCourseType vo = new VOTOCourseType();
        vo = objCourseType.Load(id);

        txt_Name.Text = vo.CourseTypeName;
        txt_Introduce.Text = vo.Description;
        ddl_XueDuan.SelectedValue = vo.Xueduan.ToString();
        ddl_XueKe.SelectedValue = vo.Xueke.ToString();
        ddl_TrainType.SelectedValue = vo.FKTrainTypeID.ToString();
        if (vo.Used)
        {
            radbtn_Yes.Checked = true;
            radbtn_No.Checked = false;
        }
        else
        {
            radbtn_Yes.Checked = false;
            radbtn_No.Checked = true;
        }

        BinderCheckBoxList(vo.Xueduan, vo.Xueke,vo.FKTrainTypeID);



        Hashtable hs = new Hashtable();
        //ArrayList list = new ArrayList();

        string[] CourseIDs = vo.CourseID.Split(',');
        for (int i = 0; i < CourseIDs.Length; i++)
        {
            hs.Add(CourseIDs[i].ToString(), "1");
        }



        for (int j = 0; j < chk_FKCourseIDlist.Items.Count; j++)
        {
            if (hs.ContainsKey(chk_FKCourseIDlist.Items[j].Value))
                chk_FKCourseIDlist.Items[j].Selected = true;
        }



    }
    private bool CheckPageData()
    {
        string search = string.Format("CourseTypeName = '{0}'", txt_Name.Text.Trim());
        if (ViewState["CTID"] != null)
        {
            search += "and CourseType_Id<>" + ViewState["CTID"].ToString();
        }
        if (objCourseType.Count(search) != 0)
        {

            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);

            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        string str_CourseIDlist = getCourseIDlist();
        if (string.IsNullOrEmpty(str_CourseIDlist))
        {
            lbl_msg_Error.Text = "<br />请选择课程资源！";
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOCourseType vo = new VOTOCourseType();
        if (ViewState["CTID"] == null)
        {
            vo.CourseTypeName = txt_Name.Text.Trim();
            vo.Description = txt_Introduce.Text.Trim();
            if (radbtn_Yes.Checked == true)
                vo.Used = true;
            if (radbtn_No.Checked == true)
                vo.Used = false;
            vo.Xueke = int.Parse(ddl_XueKe.SelectedValue);
            vo.Xueduan = int.Parse(ddl_XueDuan.SelectedValue);
            vo.FKTrainTypeID = int.Parse(ddl_TrainType.SelectedValue);
            string CourseIDlist = getCourseIDlist();
            vo.CourseID = CourseIDlist;
            if (objCourseType.Add(vo))
            {
                string url = "ManageCourseType.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = objCourseType.Load(int.Parse(ViewState["CTID"].ToString()));
            vo.CourseTypeName = txt_Name.Text.Trim();
            vo.Description = txt_Introduce.Text.Trim();
            if (radbtn_Yes.Checked == true)
                vo.Used = true;
            if (radbtn_No.Checked == true)
                vo.Used = false;
            vo.Xueke = int.Parse(ddl_XueKe.SelectedValue);
            vo.Xueduan = int.Parse(ddl_XueDuan.SelectedValue);
            vo.FKTrainTypeID = int.Parse(ddl_TrainType.SelectedValue);
            string CourseIDlist = getCourseIDlist();
            vo.CourseID = CourseIDlist;
            if (objCourseType.Update(vo))
            {
                string url = "ManageCourseType.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageCourseType.aspx";
        Script.RedirectURL(url);
    }


    protected void ddl_XueDuan_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderCheckBoxList(int.Parse(ddl_XueDuan.SelectedValue), int.Parse(ddl_XueKe.SelectedValue), int.Parse(ddl_TrainType.SelectedValue));
    }
    protected void ddl_XueKe_SelectedIndexChanged(object sender, EventArgs e)
    {
        BinderCheckBoxList(int.Parse(ddl_XueDuan.SelectedValue), int.Parse(ddl_XueKe.SelectedValue), int.Parse(ddl_TrainType.SelectedValue));
    }
}
